package com.weixin.paleituo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.SumBrokerageRecyclerAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.SumBrokerageBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 总佣金
 *
 * @author fengzhen
 * @version v1.0, 2018/5/11
 */
public class SumBrokerageFragment extends BaseFragment {

    public static final int PAGE_SIZE = 20;

    @BindView(R.id.recycler_fragment_sum_brokerage)
    YRecycleview mRecycler;
    @BindView(R.id.tv_sum_brokerage_fragment)
    TextView mTvSumBrokerage;

    private int mPage = 1;
    private LoadingPagerHead mLoadingPagerHead;

    private List<SumBrokerageBean.ListBean> mData = new ArrayList<SumBrokerageBean.ListBean>();
    private SumBrokerageRecyclerAdapter mAdapter;

    public static SumBrokerageFragment newInstance() {
        Bundle args = new Bundle();
        SumBrokerageFragment fragment = new SumBrokerageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View initView(Context context) {
        mLoadingPagerHead = new LoadingPagerHead(getContext(), R.layout.layout_brokerage_empy) {
            @Override
            protected void close() {
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(getContext(), R.layout.fragment_sum_brokerage);
            }

            @Override
            protected void reLoading() {
                getData(0);
            }
        };
        return mLoadingPagerHead;
    }

    @Override
    protected void initData() {
        super.initData();

        mRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mAdapter = new SumBrokerageRecyclerAdapter(mData);
        mRecycler.setAdapter(mAdapter);
        mRecycler.setRefreshAndLoadMoreListener(new YRecycleview.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                getData(1);
            }

            @Override
            public void onLoadMore() {
                getData(2);
            }
        });

        getData(0);
    }

    /**
     * @param type 0:第一次请求数据 1：刷新数据 2：加载数据
     */
    private void getData(final int type) {
        switch (type) {
            case 0:
                mPage = 1;
                mLoadingPagerHead.showPagerView(Contans.STATE_LOADING);
                break;
            case 1:
                mPage = 1;
                break;
            case 2:
                mPage++;
                break;
        }
        Observable<StatusCode<SumBrokerageBean>> observable = ApiUtils.getApi().getSumBrokerage(
                BaseApplication.getToken(), BaseApplication.getUserId(),
                BaseApplication.sUserInfoBean.getUsername(), PAGE_SIZE, mPage);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<SumBrokerageBean>(getContext()) {
            @Override
            protected void _onNext(StatusCode<SumBrokerageBean> statusCode) {
                List<SumBrokerageBean.ListBean> data = statusCode.getData().getList();
                mTvSumBrokerage.setText(String.valueOf(statusCode.getData().getBalance()));
                switch (type) {
                    case 0:
                        if (data == null || data.size() <= 0) {
                            mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                        } else {
                            mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                            mData.clear();
                            mData.addAll(data);
                            mAdapter.notifyDataSetChanged();
                        }
                        break;
                    case 1:
                        mRecycler.setReFreshComplete();
                        if (data == null || data.size() <= 0) {
                            mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                        } else {
                            mData.clear();
                            mData.addAll(data);
                            mAdapter.notifyDataSetChanged();
                        }
                        break;
                    case 2:
                        mRecycler.setloadMoreComplete();
                        if (data == null) return;
                        mData.addAll(data);
                        break;
                }
            }

            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);
                switch (type) {
                    case 0:
                        mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                        break;
                    case 1:
                        mRecycler.setReFreshComplete();
                        break;
                    case 2:
                        mPage--;
                        mRecycler.setloadMoreComplete();
                        break;
                }
            }
        }, "", lifecycleSubject, false, true);
    }
}
